package com.xuanzheng.company.servlet;

import com.google.gson.Gson;
import com.xuanzheng.company.entity.CompanyEmployee;
import com.xuanzheng.company.entity.User;
import com.xuanzheng.company.service.CompanyEmployeeService;
import com.xuanzheng.company.service.impl.CompanyEmployeeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/checkResignationStatus")
public class CheckResignationStatusServlet extends HttpServlet {
    private CompanyEmployeeService companyEmployeeService = new CompanyEmployeeServiceImpl();
    private Gson gson = new Gson();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Map<String, Object> result = new HashMap<>();
        
        // 获取当前登录用户
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute("user") == null) {
            result.put("success", false);
            result.put("message", "请先登录");
            out.print(gson.toJson(result));
            return;
        }
        
        User currentUser = (User) session.getAttribute("user");
        
        // 检查用户是否在公司中
        if (currentUser.getCompanyId() == null || currentUser.getCompanyId().isEmpty()) {
            result.put("success", true);
            result.put("status", -1); // -1表示未加入公司
            result.put("message", "您当前未加入任何公司");
            out.print(gson.toJson(result));
            return;
        }
        
        // 获取用户的离职申请状态
        CompanyEmployee resignation = companyEmployeeService.getUserResignation(currentUser.getId());
        
        if (resignation == null) {
            // 检查用户是否有员工记录
            CompanyEmployee employee = companyEmployeeService.getByUserId(currentUser.getId());
            
            if (employee == null) {
                result.put("success", true);
                result.put("status", -2); // -2表示找不到员工记录
                result.put("message", "找不到您的公司员工记录");
            } else {
                result.put("success", true);
                result.put("status", 0); // 0表示未申请离职
                result.put("message", "您当前未申请离职");
                result.put("employee", employee);
            }
        } else {
            // 有离职申请
            result.put("success", true);
            result.put("status", resignation.getStatus()); // 3表示待审核离职申请
            result.put("message", resignation.getStatus() == 3 ? "您的离职申请正在审核中" : "您的离职申请已处理");
            result.put("resignation", resignation);
        }
        
        out.print(gson.toJson(result));
    }
} 